"""
SPDX-FileCopyrightText:  2020-2021 zPlus <zplus@peers.community>
SPDX-License-Identifier: GPL-2.0-only
"""

import flask
import forgefed
import json
import logging
import os
import pytest
import tempfile

from forgefed import APP_URL, activitypub
from flask import url_for, request
from pagure import flask_app

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

@pytest.fixture
def client():
    #db_fd, db_path = tempfile.mkstemp()
    app = flask_app.create_app()
    app.config['TESTING'] = True

    with app.test_client() as client:
        #with app.app_context():
        #    flaskr.init_db()
        yield client

    #os.close(db_fd)
    #os.unlink(db_path)

def test_project_jsonld(client):
    response = client.get('/foo/my_project', follow_redirects=True, headers=activitypub.REQUEST_HEADERS)
    data = json.loads(response.data)

    assert 'name' in data
    assert 'repository' in data
    assert 'tickettracker' in data

    result = {
        '@context': activitypub.jsonld_context,
        'type': 'Project'
    }

    assert result.items() <= data.items()

def test_person_jsonld(client):
    response = client.get('/user/foo', follow_redirects=True, headers=activitypub.REQUEST_HEADERS)
    data = json.loads(response.data)

    assert 'name' in data
    assert 'preferredUsername' in data
    assert 'roles' in data
    assert 'sshKey' in data

    result = {
        '@context': activitypub.jsonld_context,
        'followers': '{}/user/foo/followers'.format(APP_URL),
        'following': '{}/user/foo/following'.format(APP_URL),
        'id': '{}/user/foo'.format(APP_URL),
        'inbox': '{}/user/foo/inbox'.format(APP_URL),
        'outbox': '{}/user/foo/outbox'.format(APP_URL),
        'publicKey': '{}/user/foo/key.pub'.format(APP_URL),
        'type': 'Person'
    }

    assert result.items() <= data.items()
